package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Appmanager;
import com.ruoyi.system.domain.Drtypeinfo;
import com.ruoyi.system.domain.Sub;
import com.ruoyi.system.service.IAlarmtypeService;
import com.ruoyi.system.service.IAppmanagerService;
import com.ruoyi.system.service.ISubService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Drtypemode;
import com.ruoyi.system.service.IDrtypemodeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目设备属性定义Controller
 * 
 * @author yaozh
 * @date 2024-02-29
 */
@RestController
@RequestMapping("/system/drtypemode")
public class DrtypemodeController extends BaseController
{
    @Resource
    private IDrtypemodeService drtypemodeService;
    @Resource
    private IAppmanagerService appManagerService;
    @Resource
    private ISubService subService;
    @Resource
    private IAlarmtypeService alarmtypeService;

    /**
     * 查询项目设备属性定义列表
     */
    @PreAuthorize("@ss.hasPermi('system:drtypemode:list')")
    @GetMapping("/list")
    public TableDataInfo list(Drtypemode drtypemode,@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return getDataTable(new ArrayList<>());
        }
        startPage();
        List<Drtypemode> list = drtypemodeService.selectDrtypemodeList(drtypemode,dbName);
        return getDataTable(list);
    }

    /**
     * 导出项目设备属性定义列表
     */
    @PreAuthorize("@ss.hasPermi('system:drtypemode:export')")
    @Log(title = "项目设备属性定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Drtypemode drtypemode,@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        List<Drtypemode> list = new ArrayList<>();
        if(appList != null && appList.size() > 0){
            list = drtypemodeService.selectDrtypemodeList(drtypemode,dbName);
        }
        ExcelUtil<Drtypemode> util = new ExcelUtil<Drtypemode>(Drtypemode.class);
        util.exportExcel(response, list, "项目设备属性定义数据");
    }

    @Log(title = "项目设备属性定义", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:drtypemode:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, @RequestParam String dbName) throws Exception
    {
        ExcelUtil<Drtypemode> util = new ExcelUtil<Drtypemode>(Drtypemode.class);
        List<Drtypemode> drTypeModeList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = drtypemodeService.importDrTypeMode(drTypeModeList, updateSupport, operName, dbName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Drtypemode> util = new ExcelUtil<Drtypemode>(Drtypemode.class);
        util.importTemplateExcel(response, "项目设备属性定义数据");
    }

    /**
     * 获取项目设备属性定义详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:drtypemode:query')")
    @GetMapping(value = "/{typemodeid}")
    public AjaxResult getInfo(@PathVariable("typemodeid") Long typemodeid,@RequestParam String dbName)
    {
        return success(drtypemodeService.selectDrtypemodeByTypemodeid(typemodeid,dbName));
    }

    /**
     * 新增项目设备属性定义
     */
    @PreAuthorize("@ss.hasPermi('system:drtypemode:add')")
    @Log(title = "项目设备属性定义", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Drtypemode drtypemode,@RequestParam String dbName)
    {
        return toAjax(drtypemodeService.insertDrtypemode(drtypemode,dbName));
    }

    /**
     * 修改项目设备属性定义
     */
    @PreAuthorize("@ss.hasPermi('system:drtypemode:edit')")
    @Log(title = "项目设备属性定义", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Drtypemode drtypemode,@RequestParam String dbName)
    {
        return toAjax(drtypemodeService.updateDrtypemode(drtypemode,dbName));
    }

    /**
     * 删除项目设备属性定义
     */
    @PreAuthorize("@ss.hasPermi('system:drtypemode:remove')")
    @Log(title = "项目设备属性定义", businessType = BusinessType.DELETE)
	@DeleteMapping("/{typemodeids}")
    public AjaxResult remove(@PathVariable Long[] typemodeids,@RequestParam String dbName)
    {
        return toAjax(drtypemodeService.deleteDrtypemodeByTypemodeids(typemodeids,dbName));
    }

    @PreAuthorize("@ss.hasPermi('system:drtypemode:list')")
    @GetMapping("/subAllList")
    public AjaxResult subAllList(@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return success(new ArrayList<>());
        }
        return success(subService.selectSubList(new Sub(),dbName));
    }

    /**
     * 不分页，查询驱动列表
     */
    @PreAuthorize("@ss.hasPermi('system:drtypemode:list')")
    @GetMapping("/listAllAlarmType")
    public AjaxResult listAllAlarmType(@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return success(new ArrayList<>());
        }
        return success(alarmtypeService.selectAlarmtypeForAll(dbName));
    }
}
